package com.smart.mip.system.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.smart.mip.system.dao.UserRoleRepository;
import com.smart.mip.system.entity.UserRole;
/**
 * UserRole  Controller.
 * create by scaffold 2017-07-18 
 * @author codefan@sina.com
 * 用户角色设定null   
*/


@Controller
@RequestMapping("/system/userrole")
public class UserRoleController{
	
	@Autowired
	private UserRoleRepository userRoleRepository;	

    /**
     * 查询所有   用户角色设定  列表
     *
     * @param field    json中只保存需要的属性名
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     * @return {data:[]}
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list(HttpServletRequest request, HttpServletResponse response, Model model) {   
		List<UserRole> listObjects = userRoleRepository.findAll();
        if (!listObjects.isEmpty()) {
			model.addAttribute("LIST", listObjects);
		}
        return "userRoleList";
    }
    
    /**
     * 查询单个  用户角色设定 
	
	 * @param urCode  UR_CODE
     * @param catalogCode 主键
     * 
     * @param response    {@link HttpServletResponse}
     * @return {data:{}}
     */
    @RequestMapping(value = "/{urCode}", method = {RequestMethod.GET})
    public void getUserRole(@PathVariable String urCode, HttpServletResponse response, Model model) {
    	
    	UserRole userRole =     			
    			userRoleRepository.findOne( urCode);
        
        model.addAttribute(userRole);
    }
    
    /**
     * 新增 用户角色设定
     *
     * @param userRole  {@link UserRole}
     * @return
     */
    @RequestMapping(method = {RequestMethod.POST})
    public String createUserRole(@RequestBody @Valid UserRole userRole, HttpServletResponse response) {
    	userRoleRepository.save(userRole);
        return "redirect:/";
    }

    /**
     * 删除单个  用户角色设定 
	
	 * @param urCode  UR_CODE
     */
    @RequestMapping(value = "/{urCode}", method = {RequestMethod.DELETE})
    public String deleteUserRole(@PathVariable String urCode, HttpServletResponse response) {
    	
    	userRoleRepository.delete( urCode);
        
       return "redirect:/";
    } 
    
    /**
     * 新增或保存 用户角色设定 
    
	 * @param urCode  UR_CODE
	 * @param userRole  {@link UserRole}
     * @param response    {@link HttpServletResponse}
     */
    @RequestMapping(value = "/{urCode}", method = {RequestMethod.PUT})
    public String updateUserRole(@PathVariable String urCode, 
    	@RequestBody @Valid UserRole userRole, HttpServletResponse response) {
    	
    	
    	UserRole dbUserRole  =     			
    			userRoleRepository.findOne( urCode);
        
        

        if (null != userRole) {
        	dbUserRole .copy(userRole);
        	userRoleRepository.saveAndFlush(dbUserRole);
        } 

        return "redirect:/";
    }
}
